package com.op.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 封装的统一返回对象
 *
 * @author 孤焰
 * @since 2021-09-24 16:39:33
 */
@Getter
@ToString
@ApiModel("结果封装实体类")
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = -69724478181135427L;

    @ApiModelProperty("状态码")
    private Integer code;

    @ApiModelProperty("状态信息")
    private String message;

    @ApiModelProperty("返回数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    private ResponseResult() {
    }

    private ResponseResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(StateCode.SUCCESS.getCode(), StateCode.SUCCESS.getMessage(), data);
    }

    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(StateCode.SUCCESS.getCode(), StateCode.SUCCESS.getMessage(), null);
    }

    public static <T> ResponseResult<T> fail() {
        return new ResponseResult<>(StateCode.FAIL.getCode(), StateCode.FAIL.getMessage(), null);
    }

    public static <T> ResponseResult<T> fail(StateCode stateCode) {
        return new ResponseResult<>(stateCode.getCode(), stateCode.getMessage(), null);
    }

    public static <T> ResponseResult<T> fail(Integer code, String message) {
        return new ResponseResult<>(code, message, null);
    }

    public static <T> ResponseResult<T> error() {
        return new ResponseResult<>(StateCode.UN_KNOW_ERROR.getCode(), StateCode.UN_KNOW_ERROR.getMessage(), null);
    }
}

